
global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_7_addcontrols.log, replace name(tabl)

capture noi {

	* Additional control regressions
	* v8

	* ------------------- *
	* Prepare baseline reg
	* ------------------- *

	estimates clear
	qui do ${code_dir}/config/tabletools.do



	use ${final_dir}/regression_dataset${weight_window}${tf}, clear
	*the usual cloning and renaming for better readability inside this file
	clonevar LSW = lswMPm_1995_a
	clonevar HSW = hswMPm_1995_a
	clonevar VAEMP = vaempMPm_1995_a
	clonevar GDPGAP = lngdpgap_1995_a
	clonevar GDPPC = gdppcMPm_1995_a

	clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
	clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a
	clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
	clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a	
	clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a

	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0  
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0

	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0

	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995
	sort lse_id year
	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearctrylintr = group(year country_shr_lintr_1995) if year <= 2009
	egen yearindustry = group(year industry) if year <= 2009
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009

	* ------------------- *
	* Manufuacturing size
	* ------------------- *

	clonevar msize = mVAm_1995_a
	clonevar msize_foreign = mVAm_shr4_foreign_1995_a
	
	* 1) Manufacturing size | F+IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP msize stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store ms_fd1
	* 4) Manufacturing size | F+IY+CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP msize stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store ms_fd2
	* 7) Manufacturing size |  F+IY+CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign msize_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store ms_f

	* ------------------- *
	* Recent Innovations
	* ------------------- *

	mmerge BvD year using ${final_dir}/bvd_year_dspillovers_${depvar}_${ttt}_tfacit1_recent_innovation.dta, unmatched(master)
	sort lse_id year
	clonevar spilloversownpw = dspill${depvar}_1995_a
	clonevar spilloversotherpw = dspillN${depvar}_1995_a
	clonevar spilloversownzeropw = dspill${depvar}_1995_a0
	clonevar spilloversotherzeropw = dspillN${depvar}_1995_a0 
	clonevar spilloversown_foreignpw = dspill${depvar}_f_shr4_1995_a 
	clonevar spilloversother_foreignpw = dspillN${depvar}_f_shr4_1995_a

	* 3) VAEMP + Recent Innovations | F+IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero spilloversownpw spilloversownzeropw spilloversotherpw spilloversotherzeropw if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store pw_fd1
	* 6) VAEMP + Recent Innovations | F+IY+CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero spilloversownpw spilloversownzeropw spilloversotherpw spilloversotherzeropw if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store pw_fd2
	* 9) VAEMP + Recent Innovations | F+IY+CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero spilloversown_foreignpw spilloversownzeropw spilloversother_foreignpw spilloversotherzeropw if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store pw_f

	* ------------------- *
	* Manuf demand shift
	* ------------------- *

	clonevar mds_control = mVAlsm_1995_a
	clonevar mds_control_foreign = mVAlsm_shr4_fg_1995_a
	* 2) VAEMP + Manufacturing size low skill weighted | F+IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP mVAlsm_1995_a stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store mds_fd1
	* 5)  VAEMP + Manufacturing size low skill weighted | F+IY+CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP mVAlsm_1995_a stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store mds_fd2
	* 8) VAEMP + Manufacturing size low skill weighted | F+IY+CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign VAEMP_foreign HSW_foreign GDPgap_foreign mVAlsm_shr4_fg_1995_a stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
	estadd local has_stockspill "\yes"
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store mds_f


	* ------------------- *
	* Output tables
	* ------------------- *
	do ${code_dir}/config/tabletools.do
	setlabels
	*formerly named table_ovb1.tex
	dhoztab ms_fd1 mds_fd1 pw_fd1 ms_fd2 mds_fd2 pw_fd2 ms_f mds_f pw_f using ${tab_dir}/main/Table_7_addcontrols.tex,  b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines showdepvarinfo nomtitles sfmt(a1) scalars("has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label mgroups("Domestic and foreign" "Foreign", pattern(1 0 0 0 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(*zero* _cons spilloversown spilloversother stock*) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC msize_foreign msize Lintr_foreign Lintr spilloversown_foreignpw spilloversownpw spilloversother_foreignpw spilloversotherpw offshoring_foreign offshoring LSW_foreigniw LSWiw HSW_foreigniw HSWiw mVAlsm_shr4_fg_1995_a mVAlsm_1995_a) ///
	order(LSW HSW GDPGAP VAEMP msize mVAlsm_1995_a spilloversownpw spilloversotherpw) ///
		depvar("Auto95") ///
		notes("This table adds additional control variables. Manufacturing size denotes the log of weighted average of manufacturing value added in the customers' countries. Manufacturing size (low skill-weighted) weighs the value added of each manufacturing sector by the low-skill share in the total labor share in the US in 1995. Recent auto95 innovation and recent other innovation denote the log weighted averages of the flow of auto95 and other innovations in the last 3 years in the customers' countries. All columns include firm and industry-year fixed effects. Columns 4--9 add country-year fixed effects. Columns 7--9 use the normalized foreign variables as defined in the text The normalized foreign manufacturing sizes and foreign innovations are defined similarly to normalized foreign low-skill wages. Standard errors are clustered at firm-level and reported in parentheses.")

	*vscode shows the } in the command as wrong, but it works
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl